Kahn's Algorithm | Tejas Sanap

KAHN'S ALGORITHM

CLOSE

Each graph has nodes and edges.

The edges can have a direction, they can either point “towards” a node or “away” from it, i.e., they are either forward or reversed.

We can use the in-degree count to determine:

An adjacency-list is one way of representing a graph. Kahn’s algorithm works by removing layers of nodes that don’t have dependencies. Therefore, the adjacency list must represent the graph in a specific way.

The adjacency list must show:

In order to perform the steps in the algorithim you need two inputs: in-degree count for each node and a graph represented in the form of an adjacency list.

So, how does Kahn’s algorithm work?

But what if, a graph doesn’t have any nodes with zero in-degrees? It means, that this graph is not a DAG. Kahn’s algorithm checks for this invariant as its very first step.

Every DAG must always have at least one node with zero in-degrees.